#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Collections.Generic;
using Newtonsoft.Utilities.Miscellaneous;
using Newtonsoft.Utilities.Testing;
using NUnit.Framework;
using System;

namespace Newtonsoft.Utilities.Tests.Miscellaneous
{
  public class MathUtilsTests : TestsBase
  {
    [Test]
    public void DivideRange()
    {
      List<Range<double>> ranges = MathUtils.DivideRange(new Range<double>(10, 19), 3);
      List<Range<double>> expected = new List<Range<double>>();
      expected.Add(new Range<double>(10, 13));
      expected.Add(new Range<double>(13, 16));
      expected.Add(new Range<double>(16, 19));
      CollectionAssert.AreEqual(expected, ranges);

      ranges = MathUtils.DivideRange(new Range<double>(10, 19), 4);
      expected = new List<Range<double>>();
      expected.Add(new Range<double>(10, 12.25));
      expected.Add(new Range<double>(12.25, 14.5));
      expected.Add(new Range<double>(14.5, 16.75));
      expected.Add(new Range<double>(16.75, 19));
      CollectionAssert.AreEqual(expected, ranges);
    }

    [Test]
    public void IsInRange()
    {
      Assert.AreEqual(true, MathUtils.InRange<int>(1, 1, 5));
      Assert.AreEqual(true, MathUtils.InRange<int>(-2, -5, -1));
      Assert.AreEqual(true, MathUtils.InRange<double>(1, 1, 5));
      Assert.AreEqual(true, MathUtils.InRange<decimal>(1, 1, 5));
      Assert.AreEqual(true, MathUtils.InRange<decimal>(-10.5m, -10.5m, -5.1m));
      Assert.AreEqual(true, MathUtils.InRange<uint>(1, uint.MinValue, uint.MaxValue));
      Assert.AreEqual(true, MathUtils.InRange<ulong>(1, ulong.MinValue, ulong.MaxValue));
      Assert.AreEqual(true, MathUtils.InRange<DateTime>(DateTime.Now, DateTime.MinValue, DateTime.MaxValue));
      Assert.AreEqual(true, MathUtils.InRange<TimeSpan>(TimeSpan.MinValue, TimeSpan.MinValue, TimeSpan.MaxValue));
      Assert.AreEqual(true, MathUtils.InRange<TimeSpan>(new TimeSpan(1000), TimeSpan.MinValue, TimeSpan.MaxValue));
      Assert.AreEqual(false, MathUtils.InRange<TimeSpan>(TimeSpan.MinValue, new TimeSpan(1000), TimeSpan.MaxValue));
      Assert.AreEqual(false, MathUtils.InRange<int>(0, -1, 0));
      Assert.AreEqual(false, MathUtils.InRange<DateTime>(DateTime.MinValue, new DateTime(1000), DateTime.MaxValue));
    }

    [Test]
    public void RangeEquals()
    {
      Range<int> r1 = new Range<int>(0, 1);
      Range<int> r2 = new Range<int>(0, 1);
      Assert.IsTrue(r1.Equals(r2));
      Assert.AreEqual(r1, r2);

      r2 = new Range<int>(0, 2);
      Assert.IsFalse(r1.Equals(r2));
      Assert.AreNotEqual(r1, r2);

      r2 = new Range<int>(-1, 1);
      Assert.IsFalse(r1.Equals(r2));
      Assert.AreNotEqual(r1, r2);
    }
  }
}